package com.ouyang.servlet.User;

import com.ouyang.poji.User;
import com.ouyang.service.User.UserService;
import com.ouyang.service.User.UserServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user.do")
public class UserServlet extends HttpServlet {

    UserService userService = new UserServiceImpl();

    @Override
    protected void doGet ( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost ( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("changepwd")) {
            this.changepwd(req, resp);
        } else if (method.equals("getuserlist")) {
            this.getuserlist(req, resp);
        } else if (method.equals("search")) {
            this.search(req, resp);
        } else if (method.equals("add")) {
            this.add(req, resp);
        } else if (method.equals("updata")) {
            this.updata(req, resp);
        }else if(method.equals("del")) {
            this.del(req, resp);
        }
    }


    public void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        userService.del(id);
        resp.sendRedirect("user.do?method=getuserlist");
    }

    public void updata ( HttpServletRequest req, HttpServletResponse resp ) throws IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        String UserName = req.getParameter("username");
        String PassWord = req.getParameter("password");
        String Email = req.getParameter("email");
        userService.updata(id,UserName,PassWord,Email);
        resp.sendRedirect("user.do?method=getuserlist");
    }

    public void add ( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
        String UserName = req.getParameter("Username");
        String Password = req.getParameter("Password");
        String Email = req.getParameter("Email");
        userService.add(UserName, Password, Email);
        resp.sendRedirect("user.do?method=getuserlist");
    }


    public void search ( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
        String key = req.getParameter("key");
        String value = req.getParameter("value");
        req.setAttribute("list", userService.search(key, value));
        req.getRequestDispatcher("/admin/userlist.jsp").forward(req, resp);
    }


    public void changepwd ( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
        Object user = req.getSession().getAttribute("usersession");
        String newpassword = req.getParameter("newpassword");
        if (user != null && newpassword != null) {
            userService = new UserServiceImpl();
            boolean flag = userService.changepwd(((User) user).getUsername(), newpassword);
            if (flag) {
                req.setAttribute("message", "修改密码成功,请退出并使用新密码重新登录！");
                req.getSession().removeAttribute("usersession");
            } else {
                req.setAttribute("message", "修改密码失败！");
            }
        } else {
            req.setAttribute("message", "修改密码失败！");
        }
//        System.out.println(user);
//        System.out.println(newpassword);
        req.getRequestDispatcher("/admin/pwdmodify.jsp").forward(req, resp);

    }

    public void getuserlist ( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
        req.setAttribute("list", userService.getuserlist());
//        System.out.println(userService.getuserlist());
        req.getRequestDispatcher("/admin/userlist.jsp").forward(req, resp);
    }

}